package cn.com.jslh.boarpro.main.presenter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;

import net.idik.lib.slimadapter.SlimAdapter;
import net.idik.lib.slimadapter.SlimAdapterEx;
import net.idik.lib.slimadapter.SlimInjector;
import net.idik.lib.slimadapter.viewinjector.IViewInjector;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.nio.file.FileAlreadyExistsException;
import java.util.ArrayList;
import java.util.List;

import cn.com.jslh.boarpro.R;
import cn.com.jslh.boarpro.base.mvp.BaseListModel;
import cn.com.jslh.boarpro.base.mvp.BaseListPresenter;
import cn.com.jslh.boarpro.base.mvp.BaseListView;
import cn.com.jslh.boarpro.main.contract.SearchResultContract;
import cn.com.jslh.boarpro.main.contract.WarnResultContract;
import cn.com.jslh.boarpro.main.entity.WarnResultBody;
import cn.com.jslh.boarpro.main.entity.WarnResultEntity;
import cn.com.jslh.boarpro.main.entity.WarnResultHead;
import cn.com.jslh.boarpro.main.model.WarnResultModelImpl;
import cn.com.jslh.boarpro.main.view.SearchItemInfoActivity;
import cn.com.jslh.boarpro.util.JSONUtil;
import cn.com.jslh.boarpro.util.StartActivityUtil;
import cn.com.jslh.boarpro.util.WebService;
import cn.com.jslh.boarpro.util.adapter.WarnResultAdapter;

/**
 * Created by lenovo on 2018/3/23.
 */

public class WarnResultPresenterImpl   {
    private Context context;
    private WarnResultBody head;
    private WarnResultEntity info;
    private WarnResultAdapter adapter;
    private boolean isErr; //是否成功获取数据
    private int pageIndex=0;
    private List<String> widthList;
    private RecyclerView recyclerView;
    private String lineId,itemId;
    private int page=1;
    private LinearLayout layout;

    public  WarnResultPresenterImpl(Context context,RecyclerView recyclerView,WarnResultAdapter adapter,LinearLayout layout,String...para){
        this.context = context;
        this.recyclerView=recyclerView;
        this.adapter=adapter;
        this.layout=layout;
       itemId=para[1];
       lineId=para[0];
    }

    public void initAdapter(){
        recyclerView.setAdapter(adapter);
        adapter.setEnableLoadMore(false);
        adapter.disableLoadMoreIfNotFullPage(recyclerView);
        recyclerView.setAdapter(adapter);
        adapter.setEnableLoadMore(false);

//        View view=LayoutInflater.from(context).inflate(R.layout.empty_layout,null);
//        adapter.setEmptyView(view);

        getData(1);


        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                Log.d("是否加载更多","是否加载");
                adapter.setUpFetchEnable(false);
                adapter.setEnableLoadMore(true);
                if(pageIndex!=0){
                    adapter.loadMoreEnd();
                }else {
                    if(isErr){
                        page+=1;
                        upData(page,false);
//                        adapter.loadMoreComplete();
//                        adapter.notifyDataSetChanged();
                    }else {
                        adapter.loadMoreFail();
                    }
                }
            }
        },recyclerView);




    }


    public void getData(final int page){
        new WarnResultModelImpl(context).getWarnResultData(new WebService.NetSuccessListener() {
            @Override
            public void Success(String s) throws JSONException {
                Log.d("s", s);
                adapter.setEnableLoadMore(false);
                info = JSON.parseObject(s, WarnResultEntity.class);
                pageIndex = info.getData().getIsLastPage();
                Log.d("pageIndex", pageIndex + "zxx");
                head = info.getData().getHead();
                setHeader();
                List<WarnResultBody> bodyList = info.getData().getBody();
                widthList = info.getData().getHead().getColWidth();
                adapter.setDwidth(widthList);
                adapter.addData(bodyList, false);
//                adapter.loadMoreComplete();

                adapter.notifyDataSetChanged();

            }
        }, new WebService.NetFaildListener() {
            @Override
            public void Faild(String str) {

            }
        },lineId, itemId, page);
    }



    public void setHeader() {
        WindowManager manager = ((Activity) context).getWindowManager();
        DisplayMetrics outMetrics = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(outMetrics);
        final int width = outMetrics.widthPixels;
        List<String> title=head.getHeadTitle();
        List<String> dwidth=head.getColWidth();
        layout.setBackgroundColor(context.getResources().getColor(R.color.red_title_bg));
        for(int i=0;i<title.size();i++){
            TextView textView=new TextView(context);
            textView.setTextSize(14);
            textView.setText(title.get(i));
            textView.setGravity(Gravity.CENTER);
            int mWidth= (int) (Double.parseDouble(dwidth.get(i)) * (width-100));
            textView.setWidth(mWidth);
            layout.addView(textView);
        }

    }



    public void loadMoreData(final SwipeRefreshLayout layout){
        Log.d("是否加载更多","是否加载");
        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {

            @Override
            public void onLoadMoreRequested() {

                layout.setRefreshing(false);
                layout.setNestedScrollingEnabled(false);
                adapter.setUpFetchEnable(false);
                adapter.setEnableLoadMore(true);
                if(pageIndex!=0){
                    adapter.loadMoreEnd();
                }else {
                    if (isErr) {
                        page += 1;
                        upData(pageIndex, false);
//                        adapter.loadMoreComplete();
//                        adapter.notifyDataSetChanged();
                    } else {
                        adapter.loadMoreFail();
                    }
                }

            }
        },recyclerView);




    }


    public void upData(int page,final boolean refresh){
        new WarnResultModelImpl(context).getWarnResultData(new WebService.NetSuccessListener() {
            @Override
            public void Success(String s) throws JSONException {
                isErr = true;
                info = JSON.parseObject(s, WarnResultEntity.class);
                pageIndex = info.getData().getIsLastPage();
                Log.d("pageIndex2", pageIndex + "zxx");
                head = info.getData().getHead();
                List<WarnResultBody> bodyList = info.getData().getBody();
                if (bodyList.size() > 0) {
                    widthList = info.getData().getHead().getColWidth();
                    adapter.setDwidth(widthList);
                    adapter.addData(bodyList, refresh);
//                    adapter.loadMoreComplete();
                    recyclerView.setAdapter(adapter);
//                    adapter.notifyDataSetChanged();
                }

            }
        }, new WebService.NetFaildListener() {
            @Override
            public void Faild(String str) {

            }
        }, lineId, itemId, page);
    }

    public void upData(final SwipeRefreshLayout sw){
        adapter.setEnableLoadMore(false);
        page=1;
        upData(page,true);
        sw.setRefreshing(false);
        adapter.setEnableLoadMore(true);
    }

}
